"""
post http://mall.lemonban.com:8108/prod/prod

Authorization bearerc667be68-2275-4dd7-8be2-fab7415739ab

"""
import os
import requests
from settings import upload_img_dir

# 登录，返回响应token
url = "http://mall.lemonban.com:8108/adminLogin"
method = "post"
req_body = {
    "principal": "student",
    "credentials": "123456a",
    "imageCode": "lemon"
}
token_resp = requests.request(method, url, json=req_body)

# 上传图片，返回响应上传的图片
upload_img = os.path.join(upload_img_dir, "test1.png")
img_resp = requests.request(method="post",
                            url="http://mall.lemonban.com:8108/admin/file/upload/img",
                            headers={"Authorization": f"bearer{token_resp.json()['access_token']}"},
                            files={"file": ("test1.png", open(upload_img, "rb"), "image/png")}
                            )


# 上传商品,返回响应产品信息
req_data = {
    "prodName": "mimi",
    "brief": "",
    "video": "",
    "prodNameEn": "mimi",
    "prodNameCn": "mimi",
    "contentEn": "<p><img id=\"currentImg\" class=\"currentImg\" style=\"top: 48px; left: 169px; width: 347px; height: 347px; cursor: pointer;\" title=\"点击查看图片来源\" src=\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic%2Faf%2F91%2F43%2Faf91439c131b18c1cae7b219d3cd4488.jpg&amp;refer=http%3A%2F%2Fup.enterdesk.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1648884639&amp;t=ffd3c1773fdbcb20844c8a51f4571147\" width=\"330\" height=\"330\" /></p>",
    "contentCn": "", "briefEn": "new_mimi", "briefCn": "new_mimi",
    "pic": "2022/03/3c1d3ba2beed4369bcb6dbab771aa236.png",
    "imgs": "2022/03/3c1d3ba2beed4369bcb6dbab771aa236.png",
    "preSellStatus": 0,
    "preSellTime": None,
    "categoryId": 294,
    "skuList": [{"price": 88,
                 "oriPrice": 100,
                 "stocks": 100,
                 "skuScore": 1,
                 "properties": "",
                 "skuName": "",
                 "prodName": "",
                 "weight": 0,
                 "volume": 0,
                 "status": 1,
                 "prodNameCn": "mimi",
                 "prodNameEn": "mimi"}],
    "tagList": [1],
    "content": "",
    "deliveryTemplateId": 1,
    "totalStocks": 10,
    "price": 0.01,
    "oriPrice": 0.01,
    "deliveryModeVo": {"hasShopDelivery": True, "hasUserPickUp": False, "hasCityDelivery": False}}

prod_resp = requests.request("post", "http://mall.lemonban.com:8108/prod/prod",
                             json=req_data, headers={"Authorization": f"bearer{token_resp.json()['access_token']}"}
                             )

print(prod_resp.text)